package com.core;

import java.util.Scanner;
import java.util.LinkedHashMap;
import java.util.Map;

public class Main {

	private static RegsService regsService;
	static {
		regsService = new RegsService();
	}

	public static void main(String[] args) throws Exception {
		// key:flid, value:PortGuidance
		// 文件数据是时间升序，LinkedHashMap按加入顺序存储，保证输出顺序是时间升序
		Map<String, PortGuidance> map = new LinkedHashMap<>();

		try (Scanner cin = new Scanner(Main.class.getClassLoader().getResourceAsStream("data20181025"))) {
			String line = null;
			while (cin.hasNextLine()) {
				line = cin.nextLine();
				String flightType = regsService.getInfo("FFID", line);

				// A：进港（和出港引导无关） D：出港
				if (flightType.endsWith("D")) {

					// 消息类型和航班唯一标识
					String type = regsService.getType(line);
					String flid = regsService.getInfo("FLID", line);
					if (!map.containsKey(flid)) {
						map.put(flid, new PortGuidance(null, null, null, null, null, null, null, "正点"));
					}

					// ------------------------------------------------按类别处理消息

					// DFME_DLYE：航班延误消息
					// DFME_FETT：航班预计时间变更消息。（DLYE讲道理该包含在FETT里的...）
					if ("DFME_DLYE".equals(type) || "DFME_FETT".equals(type)) {
						String fett = regsService.getInfo("FETT", line);
						
						PortGuidance guidance = map.get(flid);
						// 航班延误消息
						if ("DFME_DLYE".equals(type)) {
							guidance.setStatus("延误");

							// 航班预计时间变更消息：
						} else {
							if (fett == null || (guidance.getFett() != null && guidance.getFett().compareTo(fett) < 0))
								guidance.setStatus("延误");
							else
								guidance.setStatus("正点");
						}
						guidance.setFett(fett == null && guidance.getFett() != null ? guidance.getFett() : fett);
						map.put(flid, guidance);

						// DFME_CANE：航班取消消息。
						// DFOE_DFDE：动态航班删除。
						// 不直接删除，设置一个标志位，status设置为-1
					} else if ("DFME_CANE".equals(type) || "DFOE_DFDE".equals(type)) {

						PortGuidance guidance = map.get(flid);
						guidance.setStatus("取消");
						map.put(flid, guidance);

						// DFME_DEPE：航班本站起飞
						// DFME_FRTT：航班实际时间变更消息。飞机已经出港。
					} else if ("DFME_DEPE".equals(type) || "DFME_FRTT".equals(type)) {
						String frtt = regsService.getInfo("FRTT", line);
						PortGuidance guidance = map.get(flid);
						guidance.setFrtt(frtt);
						map.remove(flid);
						map.put(flid, guidance);
						
						// DFME_CKLS：航班值机柜台动态信息更新。获得航班的办理柜台
					} else if ("DFME_CKLS".equals(type)) {

						String code = regsService.getInfo("CODE", line);
						PortGuidance guidance = map.get(flid);
						guidance.setCode(code);
						map.remove(flid);
						map.put(flid, guidance);

					} else if ("DFME_DFUE".equals(type) || "DFME_AIRL".equals(type)) {
						String arno2 = regsService.getInfo("ARNO2", line);
						String arno3 = regsService.getInfo("ARNO3", line);
						String flno = regsService.getInfo("FLNO", line);
						String fett = regsService.getInfo("FETT", line);
						String sfno = regsService.getInfo("SFNO", line);
						PortGuidance guidance = map.get(flid);
						
						if (arno3 == null || "null".equals(arno3)) {
							guidance.setStopover("---");
							if(guidance.getTarget() == null || "null".equals(guidance.getTarget()))
								guidance.setTarget(arno2);
						} else {
							if(guidance.getTarget() == null || "null".equals(guidance.getTarget()))
								guidance.setTarget(arno3);
							if(guidance.getStopover() == null || "null".equals(guidance.getStopover()))
								guidance.setStopover(arno2);
						}
						guidance.setFlno(flno);
						guidance.setSfno(sfno);
						guidance.setFett(fett == null && guidance.getFett() != null ? guidance.getFett() : fett);
						map.remove(flid);
						map.put(flid, guidance);
					}
				}
			}
		}
		regsService.printInfos(map);
	}
}